package cn.mmko.domain.order.service;

import cn.mmko.domain.order.adapter.port.IProductPort;
import cn.mmko.domain.order.adapter.repository.IOrderRepository;
import cn.mmko.domain.order.model.aggregate.CreateOrderAggregate;
import cn.mmko.domain.order.model.entity.OrderEntity;
import cn.mmko.domain.order.model.entity.PayOrderEntity;
import cn.mmko.domain.order.model.entity.ProductEntity;
import cn.mmko.domain.order.model.entity.ShopCartEntity;
import cn.mmko.domain.order.model.valobj.OrderStatusVO;
import cn.mmko.types.common.Constants;
import com.alipay.api.AlipayApiException;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;

@Slf4j
public abstract class AbstractOrderService implements IOrderService{
    protected final IProductPort port;
    protected final IOrderRepository repository;

    protected AbstractOrderService(IProductPort port, IOrderRepository repository) {
        this.port = port;
        this.repository = repository;
    }


    @Override
    public PayOrderEntity createOrder(ShopCartEntity shopCartEntity) throws Exception {
//        查询调单和未支付订单
        OrderEntity unpaidOrderEntity = repository.queryUnpaidOrder(shopCartEntity);
        if (null!=unpaidOrderEntity&& OrderStatusVO.PAY_WAIT.equals(unpaidOrderEntity.getOrderStatusVO())){
            log.info("创建订单-存在，已存在未支付订单。userId:{}productId:{} orderId:{}",shopCartEntity.getUserId(),
                    shopCartEntity.getProductId(),unpaidOrderEntity.getOrderId());
            return PayOrderEntity.builder()
                    .orderId(unpaidOrderEntity.getOrderId())
                    .payUrl(unpaidOrderEntity.getPayUrl())
                    .build();
        } else if (null!=unpaidOrderEntity&&Constants.OrderStatusEnum.CREATE.equals(unpaidOrderEntity.getOrderStatusVO())) {
            log.info("创建订单-存在，已存在未支付订单。userId:{}productId:{} orderId:{}",shopCartEntity.getUserId(),shopCartEntity.getProductId(),unpaidOrderEntity.getOrderId());
            PayOrderEntity payOrderEntity =doPrepayOrder(shopCartEntity.getUserId(),shopCartEntity.getProductId(), unpaidOrderEntity.getProductName(),unpaidOrderEntity.getOrderId(),unpaidOrderEntity.getTotalAmount());
            return PayOrderEntity.builder()
                    .orderId(payOrderEntity.getOrderId())
                    .payUrl(payOrderEntity.getPayUrl())
                    .build();
//            //todo 超时处理
        }
        ProductEntity productEntity =port.queryProductByProductId(shopCartEntity.getProductId());
        OrderEntity orderEntity = CreateOrderAggregate.buildOrderEntity(productEntity.getProductId(), productEntity.getProductName());
        CreateOrderAggregate orderAggregate = CreateOrderAggregate.builder()
                .userId(shopCartEntity.getUserId())
                .productEntity(productEntity)
                .orderEntity(orderEntity)
                .build();
        this.doSaveOrder(orderAggregate);
        PayOrderEntity payOrderEntity=doPrepayOrder(shopCartEntity.getUserId(),productEntity.getProductId(),productEntity.getProductName(),orderEntity.getOrderId(),productEntity.getPrice());
        log.info("创建订单-不存在，创建新订单。userId:{}productId:{} orderId:{}",shopCartEntity.getUserId(),shopCartEntity.getProductId(),orderEntity.getOrderId());
        return PayOrderEntity.builder()
                .orderId(orderEntity.getOrderId())
                .payUrl(payOrderEntity.getPayUrl())
                .build();
    }



    protected abstract void doSaveOrder(CreateOrderAggregate orderAggregate);
    protected abstract PayOrderEntity doPrepayOrder(String userId, String productId, String productName, String orderId, BigDecimal totalAmount) throws AlipayApiException;
}
